package presentation.util;

import javax.el.ELContext;
import javax.faces.context.FacesContext;
import javax.faces.event.PhaseEvent;
import javax.faces.event.PhaseId;
import javax.faces.event.PhaseListener;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import presentation.view_helpers.SubscriptionBean;

/** partially taken from 
 * http://jsfatwork.irian.at/semistatic/jsf.html#!idx:/jsf.html:fig:value-event-method-reg
 * 
 */

public class DebugPhaseListener implements PhaseListener {
	private static final long serialVersionUID = 1L;
	private static Logger logger = LoggerFactory.getLogger(DebugPhaseListener.class);
	
	public void afterPhase(PhaseEvent event){
		logger.debug("After Phase: {}", event.getPhaseId());
		FacesContext fctx = FacesContext.getCurrentInstance();
		ELContext elctx = fctx.getELContext();
		SubscriptionBean subscriptionBean = (SubscriptionBean) fctx.getELContext().getELResolver().getValue(elctx, null, "subscriptionBean");
		if (subscriptionBean != null) {
			if (subscriptionBean.getCurrentSubscription() != null) {
				if (event.getPhaseId()==PhaseId.UPDATE_MODEL_VALUES)
					System.out.println("subscriptionBean Model NOW contains costUnit=" + subscriptionBean.getCurrentSubscription().getCostUnit());
			}
		}
		
		
	}
	
	public void beforePhase(PhaseEvent event){
		logger.debug("Before Phase: {}", event.getPhaseId());
	}
	
	public PhaseId getPhaseId(){
		return PhaseId.ANY_PHASE;		
	}
}